/**
 * @author Yura Zhloba
 */

trace(' ');
trace('=========');
trace('example application for rtmp-service-mate-ext');
trace('=========');

load("Message.asc");

var rooms = ['Red Room', 'Green Room', 'Blue Room'];

application.onAppStart = function()
{
	trace('onAppStart');
	application.registerClass("Message", Message);
}

application.onConnect = function(client)
{
	trace('onConnect ' + client.id + ' ' + client.referrer);

	application.acceptConnection(client);
}

application.onDisconnect = function(client)
{
	trace('onDisconnect ' + client.id);
}

Client.prototype.login = function(userName)
{
	trace('login [' + userName + ']');

	// TODO check is userName unique

	this.userName = userName;

	return userName;
}

Client.prototype.getRooms = function()
{
	return rooms;
}

Client.prototype.joinRoom = function(roomName)
{
	trace('join [' + roomName + ']');

	return roomName;
}

Client.prototype.sendMessage = function(message)
{
	trace('sendMessage [' + message.senderName + ' | ' + message.content + ']');

	var newMessage = new Message();
	newMessage.senderName = "<" + message.senderName + ">";
	newMessage.content = "|" + message.content + "|";

	application.broadcastMsg('newMessage', newMessage);

	return true;
}